'use strict';

const Controller = require('egg').Controller;

class UtilsController extends Controller {
  async getAllStaff() {
    const ctx = this.ctx;

    try {
      const users = await ctx.service.user.getAllStaff();
      this.ctx.body = {
        code: 200,
        users,
      };
    } catch (e) {
      this.ctx.body = {
        code: 500,
        msg: '服务端出错',
      };
    }
  }

  async getAllUsers() {
    const ctx = this.ctx;

    try {
      const users = await ctx.service.user.getAllUsers();
      this.ctx.body = {
        code: 200,
        users,
      };
    } catch (e) {
      this.ctx.body = {
        code: 500,
        msg: '服务端出错',
      };
    }
  }

  async setPermission() {
    const ctx = this.ctx;
    const body = ctx.request.body;
    const { user_id, permission } = body;
    try {
      const user = await ctx.service.user.setPermission(user_id, permission);
      this.ctx.body = {
        code: 200,
        user,
        msg: '权限设置成功',
      };
    } catch (e) {
      this.ctx.body = {
        code: 500,
        msg: '服务端出错',
      };
    }
  }
}

module.exports = UtilsController;
